
<?php

define("TOKEN", "weixin");                    
$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
	$wechatObj->responseMsg();
}else{
    $wechatObj->valid();
}

class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if($tmpStr == $signature){
            return true;
        }else{
            return false;
        }
    }

    
    
    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);

          
            switch ($RX_TYPE)
            {
                case "text":
                    $result = $this->receiveText($postObj);
                    break;
                case "image":
                    $result = $this->receiveImage($postObj);
                    break;
                case "voice":
                    $result = $this->receiveVoice($postObj);
                    break;
                case "video":
                    $result = $this->receiveVideo($postObj);
                    break;
                default:
                    $result = "unknow msg type: ".$RX_TYPE;
                    break;
            }
            echo $result;
        }else {
            echo "";
            exit;
        }
    }
    
    
    
        
        //计算天数
    function countDays($obj){
    	$start = time();
        $end = strtotime($obj);
        $s = $end - $start;
        $d = $s / 86400;
        return floor($d) . "天";
    }
    
    //计算天数和小时
    function countDaysHours($obj){
    	$start = time();
        $end = strtotime($obj);
        $s = abs($end - $start);
        $d = $s / 86400;
        $h = $s % 86400 / 3600;
        return floor($d) . "天" . floor($h) . "时";
    }
    
    //计算时间差
    function countTime2($obj){
    	$start = time();
        $end = strtotime($obj);
        $s = abs($end - $start);
        $h = $s / 3600;
        $m = $s % 3600 / 60;
        $s = $s % 60;
        return floor($h) . "时" . floor($m) . "分" .$s ."秒";
    }
    
    
    
    function countTime3($obj){
    	$start = time();
        $end = strtotime($obj);
        $s = abs($end - $start);
        $h = $s / 3600;
        $m = $s % 3600 / 60;
        $s = $s % 60;
         return floor($h) . "时" . floor($m) . "分" .$s ."秒";
    }
    
    //计算时间差
    function countTime($obj){
    	$start = time();
        $end = strtotime($obj);
        $s = abs($end - $start);
        $d = $s / 86400;
        $h = $s % 86400  / 3600;
        $m = $s % 3600 / 60;
        $s = $s % 60;
        return floor($d) . "天" . floor($h) . "时" . floor($m) . "分" .$s ."秒";
    }
    
    //计算时间差
    function countTime4($obj){
    	$start = time();
        $end = strtotime($obj);
        $s = abs($end - $start);
        $d = $s / 86400;
        $h = $s % 86400  / 3600;
        $m = $s % 3600 / 60;
        $s = $s % 60;
        return floor($d) . "天" . floor($h) . "时" . floor($m) . "分" .$s ."秒";
    }
    
    //计算下课还剩多少秒
    function countOverClassTime(){
        $now = time();
        
        if(date("N",$now) == 6 || date("N",$now) == 7){
        	return "";
        } 
      
        if($now - strtotime("8:30") >= 0 && strtotime("9:15") - $now >= 0){
        	return "距离 下课 还有 ". $this->countTime3("9:15") . "\n";
        }else if($now - strtotime("09:20") >= 0 && strtotime("10:05") - $now >= 0){
        	return "距离 上课 还有 ". $this->countTime3("10:05") . "\n";
        }else if($now - strtotime("10:20") >= 0 && strtotime("11:05") - $now >= 0){
        	return "距离 下课 还有 ". $this->countTime3("11:05")  . "\n";
        }else if($now - strtotime("11:10") >= 0 && strtotime("11:55") - $now >= 0){
        	return "距离 上课 还有 ". $this->countTime3("11:55") . "\n";;
        }else if($now - strtotime("14:10") >= 0 && strtotime("14:55") - $now >= 0){
        	return "距离 下课 还有 ". $this->countTime3("14:55")  . "\n";
		}else if($now - strtotime("15:00") >= 0 && strtotime("15:45") - $now >= 0){
        	return "距离 下课 还有 ". $this->countTime3("15:45")  . "\n";
		}else if($now - strtotime("16:00") >= 0 && strtotime("16:45") - $now >= 0){
        	return "距离 下课 还有 ". $this->countTime3("16:45")  . "\n";
       }else if($now - strtotime("16:50") >= 0 && strtotime("17:35") - $now >= 0){
        	return "距离 下课 还有 ". $this->countTime3("17:45")  . "\n";
        }else {
        	return "";    
        }
    }
    
   
    
    
   
    //放假倒计时
    function countHoliday(){
    	
        
        
          
       
        
        
        $h = date("H");
        $i = date("i");
        $s = date("s");
        
        $d=3600*$h+60*$i+$s;
        $r=(100*$d)/(24*3600);
        
        
        
      
        $res = "今天已过去 "  .floor($r)."%% \n\n";
        
        
    	$res = $res . $this->countOverClassTime();
        
        
        if(date("w") == 6 || date("w") == 0){
        	$now = time();
            $gone = strtotime("monday") - $now;
            $r = ($gone*100) / (24*3600*2);
            $res = $res . "周末还剩下 "  .floor($r)."%%\n";
        }else{
        	$res = $res ."距离 周末 还有 ". $this->countTime2("saturday")."\n";
        }
        
        if(strtotime("2016-06-18 09:00") - time() > 0){
        	$res = $res . "\n" ."距离 英语四级考试 还有 ". $this->countTime4("2016-06-18 09:00") ."\n";
        }
        
        if(strtotime("2016-06-18 15:00") - time() > 0){
        	$res = $res . "\n" ."距离 英语六级考试 还有 ". $this->countTime4("2016-06-18 15:00") ."\n";
        }
        
        if(strtotime("2016-04-30 0:00") - time() > 24*3600){
        	$res = $res . "\n" ."距离 五一小长假 还有 ". $this->countDaysHours("2016-04-30 0:00")."\n";
        }else if(strtotime("2016-04-30 0:00") - time() > 0){
        	$res = $res . "\n" ."距离 端午(3天) 还有 ". $this->countTime2("2016-04-30 0:00")."\n";
        }else if(strtotime("2016-05-02 0:00") - time() > 0){
            $now = time();
            $gone = strtotime("2016-05-02 0:00") - $now;
            $r = ($gone*100) / (24*3600*3);
            $res = $res . "\n\n五一小长假还剩下 "  .floor($r)."%% \n";
        }

		if(strtotime("2016-06-09 0:00") - time() > 24*3600){
        	$res = $res . "\n" ."距离 端午小长假 还有 ". $this->countDaysHours("2016-06-09 0:00")."\n";
        }else if(strtotime("2016-04-30 0:00") - time() > 0){
        	$res = $res . "\n" ."距离 端午小长假 还有 ". $this->countTime2("2016-06-09 0:00")."\n";
        }else if(strtotime("2016-06-11 0:00") - time() > 0){
            $now = time();
            $gone = strtotime("2016-06-11 0:00") - $now;
            $r = ($gone*100) / (24*3600*3);
            $res = $res . "\n\n端午还剩下 "  .floor($r)."%% \n";
        }
        
        
        if(strtotime("2016-07-09 0:00") - time() > 24*3600){
        	$res = $res . "\n" . "距离 暑假  还有 ". $this->countDaysHours("2016-07-09 0:00")."\n";
        }else if(strtotime("2016-07-09 0:00") - time() > 0){
        	$res = $res . "\n" . "距离 暑假 还有 ". $this->countTime2("2016-07-09 0:00")."\n";
        }else if(strtotime("2016-08-28 0:00") - time() > 0){
            $now = time();
            $gone = strtotime("2016-08-28 0:00") - $now;
            $r = ($gone*100) / (24*3600*38);
            $res = $res . "\n\n暑假还剩下 "  .floor($r)."%%";
        }
        
       
        
        if(strtotime("2017-01-01 0:00") - time() > 0){
        	$now = time();
            $gone = strtotime("2017-01-01 0:00") - $now;
            $r = ($gone*100) / (24*3600*365);
            $res = $res . "\n\n2016年还剩下 "  .floor($r)."%%";
        }
        
         
        
          
       return $res;
    }
    
    
    
	

	private function receiveText($object)
    {
        
        
     
        $keyword = trim($object->Content);
        
        $openid = $object->FromUserName;
        
        
  
         $weekarray=array("日","一","二","三","四","五","六");
   
                                         
			
             
              
		           $now = date('H:i:s',time());
                   $days = $this->countHoliday();
             
             
             $date_of_firstday='2016-2-28';
    //开学第一天的时间戳 
    $year = substr($date_of_firstday,0,4); 
    $month = substr($date_of_firstday,5,1); 
    $day = substr($date_of_firstday,7,2); 
    $time_chuo_of_first_day = mktime(0,0,0,$month,$day,$year); 
    //今天的时间戳 
    $month = date('n'); //获取月 n 
    $day = date('d');   //获取日 d 
    $year = date('Y');  //获取年 Y 
    $time_chuo_of_current_day = mktime(0,0,0,$month,$day,$year); 
    $cha = ($time_chuo_of_current_day-$time_chuo_of_first_day)/60/60/24; 
    $zhou = (int)(($cha)/7 +1); 
        
		   
             
             
              $url="";
             
              $content = array();
             
              $content[] = array("Title"=>"现在是".date('Y-m-d H:i:s',time()), 
										"Description"=>"", 
										"PicUrl"=>"http://weixin.lsu.edu.cn/djs.jpg", 
										  "Url" =>"");
			$content[] = array("Title"=>"今天是星期".$weekarray[date('w')]."\n本周是这个学期第".$zhou."周", 
										"Description"=>"", 
										"PicUrl"=>"", 
										  "Url" =>$url);
            
			
             $content[] = array("Title"=>$days, 
										"Description"=>"", 
										"PicUrl"=>"", 
										  "Url" =>"");
            
            
             
      
             
                         $result = $this->transmitNews($object, $content);

				
		
	   
	   
       
        
       
	  
        
        
        
        
        
        
        return $result;
        
       
        
    }

    
    
    
    
    
    private function receiveImage($object)
    {
       
        $content = array("MediaId"=>$object->MediaId);
        $result = $this->transmitImage($object, $content);;
        return $result;
    }

    private function receiveVoice($object)
    {
       
        $content = array("MediaId"=>$object->MediaId);
        $result = $this->transmitVoice($object, $content);;
        return $result;
    }

    private function receiveVideo($object)
    {
       
        $content = array("MediaId"=>$object->MediaId, "ThumbMediaId"=>$object->ThumbMediaId, "Title"=>"", "Description"=>"");
        $result = $this->transmitVideo($object, $content);;
        return $result;
    }  
    
    
    private function transmitText($object, $content)
    {
        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
        return $result;
    }
    
   
    private function transmitImage($object, $imageArray)
    {
        $itemTpl = "<Image>
    <MediaId><![CDATA[%s]]></MediaId>
</Image>";

        $item_str = sprintf($itemTpl, $imageArray['MediaId']);

        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
$item_str
</xml>";

        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $result;
    }
    
   
    private function transmitVoice($object, $voiceArray)
    {
        $itemTpl = "<Voice>
    <MediaId><![CDATA[%s]]></MediaId>
</Voice>";

        $item_str = sprintf($itemTpl, $voiceArray['MediaId']);

        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
$item_str
</xml>";

        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $result;
    }
    
    
    private function transmitVideo($object, $videoArray)
    {
        $itemTpl = "<Video>
    <MediaId><![CDATA[%s]]></MediaId>
    <ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
    <Title><![CDATA[%s]]></Title>
    <Description><![CDATA[%s]]></Description>
</Video>";

        $item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']);

        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
$item_str
</xml>";

        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $result;
    }
    
    
    private function transmitNews($object, $arr_item)
    {
        if(!is_array($arr_item))
            return;

        $itemTpl = "    <item>
        <Title><![CDATA[%s]]></Title>
        <Description><![CDATA[%s]]></Description>
        <PicUrl><![CDATA[%s]]></PicUrl>
        <Url><![CDATA[%s]]></Url>
    </item>
";
        $item_str = "";
        foreach ($arr_item as $item)
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);

        $newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";

        $result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
        return $result;
    }
    
   
    private function transmitMusic($object, $musicArray)
    {
        $itemTpl = "<Music>
    <Title><![CDATA[%s]]></Title>
    <Description><![CDATA[%s]]></Description>
    <MusicUrl><![CDATA[%s]]></MusicUrl>
    <HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>";

        $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);

        $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
$item_str
</xml>";

        $result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
        return $result;
    }
}
?>
	